<template>
  <div class="dashboard-editor-container">
    <panel-group :total-amount="totalAmount" :panel-map="panelMap" />
    <el-row :gutter="20">
      <el-col :span="12">
        <pie-chart
          v-if="showChart1"
          title="Order status chart"
          :series-data="numberData1"
          :legend-data="nameArr1"
          series-name="status"
        />
      </el-col>
      <el-col :span="12">
        <pie-chart
          v-if="showChart2"
          title="Order country chart"
          :series-data="numberData2"
          :legend-data="nameArr2"
          series-name="country"
        />
      </el-col>
    </el-row>
    <!-- <el-row style="background:#fff;padding:16px 16px 0;margin-bottom:32px;">
      <line-chart v-if="showChart3" :series-data="seriesData" :x-axis-date="xAxisDate" />
    </el-row> -->
  </div>
</template>

<script>
import { countryStat, statusStat, amountStat } from '@/api/common';
import PanelGroup from './PanelGroup';
import PieChart from './components/pieChart';
// import LineChart from './lineChart'
export default {
  name: 'Dashboard',
  components: {
    PanelGroup,
    // LineChart,
    PieChart
  },
  data() {
    return {
      showChart1: false,
      showChart2: false,
      showChart3: false,
      seriesData: [1, 2, 3, 4, 5, 6],
      xAxisDate: [1, 5, 10, 15, 30],
      numberData1: [],
      nameArr1: [],
      numberData2: [],
      nameArr2: [],
      totalAmount: 0,
      panelMap: {}
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.showChart3 = true;
    });
    this.countryStat();
    this.statusStat();
    this.amountStat();
  },
  methods: {
    countryStat() {
      countryStat().then(res => {
        const arr = [];
        const nameArr = [];
        res.data.forEach(item => {
          nameArr.push(item.name);
          const obj = {};
          obj['value'] = item.value;
          obj['name'] = item.name || 'unknown';
          arr.push(obj);
        });
        this.numberData2 = arr;
        this.nameArr2 = nameArr;
        this.showChart2 = true;
      });
    },
    statusStat() {
      statusStat().then(res => {
        const arr = [];
        const nameArr = [];
        const panelMap = {};
        let total = 0;
        res.data.forEach(item => {
          total += item.value;
          panelMap[item.name] = item.value;
          nameArr.push(item.name);
          const obj = {};
          obj['value'] = item.value;
          obj['name'] = item.name;
          arr.push(obj);
        });
        panelMap['orders'] = total;
        this.panelMap = panelMap;
        this.numberData1 = arr;
        this.nameArr1 = nameArr;
        this.showChart1 = true;
      });
    },
    amountStat() {
      amountStat().then(res => {
        this.totalAmount = res.data;
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.dashboard-editor-container {
  padding: 32px;
  // background-color: #eef1f6;
  position: relative;

  .github-corner {
    position: absolute;
    top: 0px;
    border: 0;
    right: 0;
  }

  .chart-wrapper {
    background: #fff;
    padding: 16px 16px 0;
    margin-bottom: 32px;
  }
}

@media (max-width: 1024px) {
  .chart-wrapper {
    padding: 8px;
  }
}
</style>
